#include "lvgl/lvgl.h"
#include <stdio.h>
#include  <math.h>

#define CHAR_PORT_ZISE 100
extern const lv_img_dsc_t plane;
extern const lv_img_dsc_t ddcar;
static lv_obj_t * result_label;
static lv_obj_t * result_label1;
static lv_obj_t * holder_page;
static lv_obj_t * bar1;
static lv_obj_t * bar2;
static lv_obj_t * bar3;
static lv_obj_t * bar4;
static lv_obj_t * led1;

static lv_obj_t* spinbox;
static lv_obj_t* chart;
static lv_chart_series_t* ser1;
static lv_obj_t* slider_lable;
static lv_obj_t * img1;
static struct {
    double dec;
    double w;
}chart_ctrl = {
    .dec = 0,
    .w = 0.1
};

static void roller_cb(struct _lv_obj_t * obj, lv_event_t event);
static void slider_cb(struct _lv_obj_t * obj, lv_event_t event);
static void page_task(lv_task_t * param);
void create_main_menu(void)
{
    lv_coord_t hres = lv_disp_get_hor_res(NULL);
    lv_coord_t vres = lv_disp_get_ver_res(NULL);
    holder_page = lv_obj_create(lv_disp_get_scr_act(NULL), NULL);
    lv_obj_set_size(holder_page, hres, vres);
    //lv_page_set_style(holder_page, LV_PAGE_STYLE_BG, &lv_style_pretty_color);
    //lv_page_set_style(holder_page, LV_PAGE_STYLE_SCRL, &lv_style_pretty_color);
    //lv_page_set_scrl_layout(holder_page, LV_LAYOUT_PRETTY);

    result_label = lv_label_create(holder_page, NULL);
    lv_label_set_text(result_label, "Title Label");
    lv_obj_align(result_label, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 20); /*Align to the top*/

    result_label1 = lv_label_create(holder_page, NULL);
    lv_label_set_text(result_label1, "");
    lv_obj_align(result_label1, NULL, LV_ALIGN_IN_TOP_LEFT, 350, 20); /*Align to the top*/

    lv_obj_t *roller1 = lv_roller_create(lv_scr_act(), NULL);
    lv_roller_set_style(roller1, LV_ROLLER_STYLE_BG, &lv_style_btn_tgl_rel);
    lv_roller_set_options(roller1,
                         LV_SYMBOL_WIFI "Apple\n" LV_SYMBOL_DRIVE"Broccoli\n" LV_SYMBOL_MUTE"Cabbage\n" LV_SYMBOL_USB"Dewberry\n" LV_SYMBOL_PLAY"Eggplant\n" "Fig\n" "Grapefruit",
                          LV_ROLLER_MODE_INIFINITE);
    lv_obj_set_event_cb(roller1, roller_cb);
    lv_obj_set_pos(roller1, 50, 80);

    bar1 = lv_bar_create(lv_scr_act(), NULL);
    lv_bar_set_style(bar1, LV_BAR_STYLE_BG, &lv_style_transp_fit);
    lv_obj_set_size(bar1, 200, 30);
    lv_obj_align(bar1, NULL, LV_ALIGN_IN_TOP_RIGHT, -20, 30);
    lv_bar_set_value(bar1, 70, LV_ANIM_ON);

    bar2 = lv_bar_create(lv_scr_act(), NULL);
    lv_bar_set_style(bar2, LV_BAR_STYLE_INDIC, &lv_style_pretty_color);
    lv_obj_set_size(bar2, 200, 30);
    lv_obj_align(bar2, NULL, LV_ALIGN_IN_TOP_RIGHT, -20, 70);
    lv_bar_set_value(bar2, 70, LV_ANIM_ON);

    static lv_style_t style_bar3;
    bar3 = lv_bar_create(lv_scr_act(), NULL);
    lv_style_copy(&style_bar3, &lv_style_plain_color);
    style_bar3.body.main_color = LV_COLOR_ORANGE;
    lv_bar_set_style(bar3, LV_BAR_STYLE_BG, &style_bar3);
    lv_obj_set_size(bar3, 200, 30);
    lv_obj_align(bar3, NULL, LV_ALIGN_IN_TOP_RIGHT, -20, 110);
    lv_bar_set_value(bar3, 70, LV_ANIM_ON);

    static lv_style_t style_bar4;
    bar4 = lv_bar_create(lv_scr_act(), NULL);
    lv_style_copy(&style_bar4, &lv_style_plain_color);
    style_bar4.body.grad_color = LV_COLOR_LIME;
    style_bar4.body.radius = 5;
    //style_bar4.body.border.part = LV_BORDER_BOTTOM;
    style_bar4.body.main_color = LV_COLOR_GREEN;
    lv_bar_set_style(bar4, LV_BAR_STYLE_INDIC, &style_bar4);
    lv_obj_set_size(bar4, 200, 30);
    lv_obj_align(bar4, NULL, LV_ALIGN_IN_TOP_RIGHT, -20, 150);
    lv_bar_set_value(bar4, 70, LV_ANIM_ON);

    lv_task_create(page_task, 200, LV_TASK_PRIO_MID, NULL);

    /*Create a style for the LED*/
    static lv_style_t style_led;
    lv_style_copy(&style_led, &lv_style_pretty_color);
    style_led.body.radius = LV_RADIUS_CIRCLE;
    style_led.body.main_color = LV_COLOR_MAKE(0xb5, 0x0f, 0x04);
    style_led.body.grad_color = LV_COLOR_MAKE(0x50, 0x07, 0x02);
    style_led.body.border.color = LV_COLOR_MAKE(0xfa, 0x0f, 0x00);
    style_led.body.border.width = 3;
    style_led.body.border.opa = LV_OPA_30;
    style_led.body.shadow.color = LV_COLOR_MAKE(0xb5, 0x0f, 0x04);
    style_led.body.shadow.width = 10;
    /*Create a LED and switch it ON*/
    led1 = lv_led_create(lv_scr_act(), NULL);
    lv_obj_set_style(led1, &style_led);
    lv_obj_align(led1, NULL, LV_ALIGN_IN_TOP_MID, 40, 200);
    lv_led_on(led1);

    static lv_style_t spinBoxStyle;
    lv_style_copy(&spinBoxStyle, &lv_style_pretty_color);
    spinbox = lv_spinbox_create(lv_scr_act(), NULL);
    lv_spinbox_set_value(spinbox,32);
    lv_spinbox_set_style(spinbox, LV_SPINBOX_STYLE_BG, &spinBoxStyle);
    //lv_spinbox_set_style(spinbox, LV_SPINBOX_STYLE_CURSOR, &spinBoxCursorStyle);
    lv_obj_set_size(spinbox, 160, 80);
    lv_obj_align(spinbox, NULL, LV_ALIGN_IN_TOP_LEFT, 4, 200);

    /*Create a default slider*/
    lv_obj_t * slider1 = lv_slider_create(lv_scr_act(), NULL);
    lv_obj_set_size(slider1, 160, 30);
    lv_obj_align(slider1, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 430, -180);
    lv_obj_set_event_cb(slider1, slider_cb);
    lv_slider_set_value(slider1, 1, LV_ANIM_ON);
    lv_slider_set_range(slider1,1,50);

    slider_lable = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(slider_lable, "0.1");
    lv_obj_align(slider_lable, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 600, -180); /*Align to the top*/

    /*chart*/
    chart = lv_chart_create(lv_scr_act(),NULL);
    lv_chart_set_update_mode(chart, LV_CHART_UPDATE_MODE_SHIFT);
    lv_chart_set_point_count(chart, CHAR_PORT_ZISE);
    lv_obj_set_size(chart, 400, 200);
    lv_chart_set_div_line_count(chart, 5, 10);
    lv_obj_align(chart, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 20, 0);
    ser1 = lv_chart_add_series(chart, LV_COLOR_RED);
    lv_chart_set_series_opa(chart, LV_OPA_70);
    lv_chart_set_range(chart,-50,50);
    lv_chart_set_type(chart, LV_CHART_TYPE_LINE | LV_CHART_TYPE_POINT);

    img1 = lv_img_create(lv_scr_act(), NULL);
    lv_img_set_src(img1, &plane);
    lv_obj_align(img1, NULL, LV_ALIGN_IN_TOP_LEFT, 220, 40);
}

static void draw_chart(void)
{
    for(short i = 0;i < 10;i++) {
        if (chart_ctrl.dec >= 360.0) {
            chart_ctrl.dec = 0;
        }
        lv_chart_set_next(chart, ser1,(lv_coord_t)(50 * sin(chart_ctrl.dec / (2 * 3.1415))));
        chart_ctrl.dec += chart_ctrl.w;
    }
    lv_chart_refresh(chart); /*Required after direct set*/
}

static void roller_cb(struct _lv_obj_t * obj, lv_event_t event)
{
    char buf[30];
    if (event == LV_EVENT_VALUE_CHANGED) {
        lv_roller_get_selected_str(obj, buf, 30);
        lv_label_set_text(result_label, buf);
    }
    lv_chart_refresh(chart); /*Required after direct set*/
}

static void slider_cb(struct _lv_obj_t * obj, lv_event_t event)
{
    int16_t per;
    char buf[10];
    if (event == LV_EVENT_VALUE_CHANGED) {
        per = lv_slider_get_value(obj);
        chart_ctrl.w = per / 10.0;
        sprintf(buf, "%f", chart_ctrl.w);
        lv_label_set_text(slider_lable, buf);
    }
    lv_chart_refresh(chart); /*Required after direct set*/
}
/**
 * Print the memory usage periodically
 * @param param
 */
static int counter = 0;
static char dddbuf[15];
static void page_task(lv_task_t * param)
{
    static char per = 0;
    sprintf(dddbuf,"%d",++counter);
    per++;
    lv_label_set_text(result_label1, dddbuf);
    lv_bar_set_value(bar1, per, LV_ANIM_ON);
    lv_bar_set_value(bar2, per, LV_ANIM_ON);
    lv_bar_set_value(bar3, per, LV_ANIM_ON);
    lv_bar_set_value(bar4, per, LV_ANIM_ON);
    if (per % 2) {
        lv_led_off(led1);
        lv_img_set_src(img1, &plane);
    } else {
        lv_led_on(led1);
        lv_img_set_src(img1, &ddcar);
    }
    if (per == 100) {
        per = 0;
    }
    draw_chart();
}
